<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/8/23
 * Time: 23:24
 */
namespace app\admin\controller;

use app\common\controller\GetuiMsg;
use jswei\push\drive\GeTuiService;
use jswei\push\sdk\geTui\igetui\IGtTemplateTye;
use jswei\push\sdk\geTui\igetui\NotifyInfo_Type;
use jswei\push\sdk\geTui\igetui\template\IGtTransmissionTemplate;
use jswei\push\sdk\geTui\igetui\template\notify\IGtNotify;
use jswei\push\sdk\geTui\igetui\utils\AppMessageCondition;
use jswei\push\sdk\geTui\igetui\utils\OptType;

class Push extends Base
{

    protected $isSink = true;
    protected $sinkMethods = ['index'];

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 推送消息
     * @param int $type
     * @return mixed
     */
    public function index($type = 1)
    {
        $config = config('getui.');
        $push = new GeTuiService($config);
        $push->setLogoURL('https://gitee.com/uploads/69/144269_jswei.png');
        $push->setTitle('测试通知');
        $push->setBody('测试的通知他');
        $push->setLinkUrl('http://baidu.com');
        //设置通知模板
        //IGtTemplateTye::notifyInfo  打开app当setExtendedData时候透传信息
        //IGtTemplateTye::link 链接模板
        //IGtTemplateTye::transmission 透传模板
        //IGtTemplateTye::download 下载模板
        $push->setTemplateType(IGtTemplateTye::link);
        //实现的方法
        /*
         $push->sendAllAndroid();     //向所有安卓平台发送
         $push->sendAllIOS();        //向所有苹果平台发送
         $push->sendAll();           //向所有平台发送
         $push->sendOne('client_id1'); //单台设备发送
         $push->sendToUserList('');  //向指定用户列表发送['client_id1','client_id2'...]

        //其他的一些可以获取个推对象和模板进行操作更多的操作
        // 获取个推对象实例
        //$igeTui = $push->getIGeTui();
        //获取消息模板
        //$template = $push->getCurrentTemplate();*/
        //return $push->sendToUserList(['b0a1bdd6cc90e05dfa9c4104f12f175c'])->getResult();
        return $push->sendAllAndroid()->getResult();
    }

    /**
     * 推送个人
     * @param int $uid
     * @param array $payload
     * @param bool $is_client
     * @return mixed
     * @throws \Exception
     */
    public function pushOneOld($uid=0,$payload=[],$is_client=false){
        $client_id = $is_client ? $uid :(new \app\admin\model\Member())->getFieldById($uid,'client_id');
        $push = new GetuiMsg();
        $push->SendTouchuanMessageToUniAppList([$client_id],$payload,'','',$data);
        $data['payload'] = $payload;
//        $config = config('getui.');
//        $push = new GeTuiService($config);
//        $client_id = (new \app\admin\model\Member())->getFieldById($uid,'client_id');
//        $template = $this->_template($payload);
//        $push->setTemplate($template);
//        $data = $push->sendOne($client_id)->getResult();
        return $data;
    }

    /**
     * 推送到个人
     * @param int $uid
     * @param array $payload
     * @param bool $is_client
     * @return mixed
     */
    public function pushOne($uid=0,$payload=[],$is_client=false){
        try{
            $client_id = $is_client ? $uid :(new \app\admin\model\Member())->getFieldById($uid,'client_id');
            $config = config('getui.');
            $push = new GeTuiService($config);
            $template = $this->_template($payload);
            $push->setTemplate($template);
            return $push->sendOne($client_id)->getResult();
        }catch (\Exception $ex){
            return $ex;
        }
    }

    /**
     * 推送所有
     * @param array $payload
     * @param string $pushTime
     * @param array $provinceList
     * @return \Exception
     */
    public function pushAll($payload=[],$pushTime='',$provinceList=[]){
        //在用户设定的时间点进行推送，格式为年月日时分
        try{
            $pushTime = $pushTime?date('YmdHi',$pushTime):'';
            $config = config('getui.');
            $push = new GeTuiService($config);
            $template = $this->_template($payload);
            $push->setPushTime($pushTime);
            $push->setTemplate($template);
            $data = $push->sendAll($provinceList)->getResult();
            return $data;
        }catch (\Exception $ex){
            return $ex;
        }
    }

    /**
     * @param array $client_id
     * @param array $payload
     * @param string $pushTime
     * @return mixed
     * @throws \Exception
     */
    public function pushList($client_id=[],$payload=[],$pushTime=''){
        //在用户设定的时间点进行推送，格式为年月日时分
        try {
            $pushTime = $pushTime?date('YmdHi',$pushTime):'';
            $config = config('getui.');
            $push = new GeTuiService($config);
            $template = $this->_template($payload);
            $push->setPushTime($pushTime);
            $push->setTemplate($template);
            $data = $push->sendToUserList($client_id)->getResult();
            return $data;
        }catch (\Exception $ex){
            return $ex;
        }
//        return (new GetuiMsg())->SendTouchuanMessageToUniAppList($uids,$payload,'','',$out);
    }

    /**
     * @param array $payload
     * @param $intent
     * @param string $APPID
     * @param string $APPKEY
     * @return IGtTransmissionTemplate
     */
    public function _template($payload=[],$APPID='',$APPKEY='',$intent=null){
        $config = config('getui.');
        $APPID = $APPID?$APPID:$config['AppID'];
        $APPKEY = $APPKEY?$APPKEY:$config['AppKey'];
        $intent = $intent??'intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=com.jiexunet/io.dcloud.UNI70C8F6C;S.UP-OL-SU=true;S.title='.$payload['title'].';S.content='.$payload['content'].';S.payload='.json_encode($payload).';end';
        $template = new IGtTransmissionTemplate();
        $template->set_appId($APPID);
        $template->set_appkey($APPKEY);
        $template->set_transmissionType(2);
        $template->set_transmissionContent(json_encode($payload));
        $notify = new IGtNotify();
        $notify -> set_title($payload['title']);
        $notify -> set_content($payload['content']);
        $notify -> set_intent($intent);
        $notify -> set_type(NotifyInfo_Type::_intent);
        $template ->set3rdNotifyInfo($notify);
        return $template;
    }
}